package zdream.control.foe.mm2pipi;

import zdream.control.world.buff.AttackModule;
import zdream.control.world.buff.HealthModule;
import zdream.control.world.MFoe;
import zdream.control.world.buff.StageModule;
import zdream.control.world.TerrainHandler;
import zdream.control.world.Ticker;

import static zdream.control.world.MFoeConstants.*;

/**
 * <p>2 代飞鸟带着的蛋里面藏着的小鸟.
 * <p>
 * <li>刚出来时四散跑, 速度为 4 块/秒
 * <li>0.5秒后, 寻找最近的攻击目标位置, 朝着这个位置不转向地跑. 速度仍为 4 块/秒
 * <li>生命值: 1, 碰撞伤害: 3, 碰撞等级: 1
 * </li></p>
 * 
 * @author Zdream
 * @since 0.0.1
 * @date 2020-06-11 (created)
 * @date 2022-06-11 (last modified)
 */
public class MM2PipiChick extends MFoe {
	public static final String NAME = "MM2PipiChick";

	public MM2PipiChick(MM2PipiEgg parent) {
		super(parent.world);
		this.type = TYPE_FOE;
		this.side = parent.side;
	}

	@Override
	public String foeName() {
		return NAME;
	}

	@Override
	public void init() {
		createNormalPattern()
				.withRectangle(-0.2917f, -0.25f, 0.5833f, 0.5f)
				.apply();

		attackModule = buildAttackModule()
				.ignoreSide(side)
				.targetTypesIsEntity()
				.create();
		healthModule = buildHealthModule()
				.outsideDestroyThreshold(1000)
				.hpMax(1)
				.create();
		stageModule = buildStageModule()
				.defineInitStage(STAGE_BEGIN, this::handleStageBegin)
				.defineStage(STAGE_SEARCH, this::handleStageSearch)
				.defineStage(STAGE_END, this::handleStageEnd)
				.create();

		terrainHandler = new TerrainHandler(world);
		terrainHandler.floating = true;
	}

	public AttackModule attackModule;
	public HealthModule healthModule;
	public StageModule stageModule;

	/* **********
	 *   阶段   *
	 ********** */
	public static final String STAGE_BEGIN = "begin";
	public static final String STAGE_SEARCH = "search";
	public static final String STAGE_END = "end";

	private void handleStageBegin(StageModule s, Ticker ticker) {
		setVelocity(vx * ticker.duration, vy * ticker.duration);
		terrainHandler.submitFloatBoxMotion(this);
		if (s.stageDuration >= 500) {
			s.changeStageTo(STAGE_SEARCH);
		}
	}

	private void handleStageSearch(StageModule s, Ticker ticker) {
		MFoe enemy = s.findNearestEnemyInRoom();
		if (enemy != null) {
			float px = anchorX, py = anchorY;
			float targetPx = enemy.getCenterX(), targetPy = enemy.getCenterY();

			if (px == targetPx) {
				// box.orientation 不变
				if (py != targetPy) { // 位置不重合
					this.vx = 0;
					this.vy = (targetPy > py) ? 4 : -4;
				}
			} else {
				float deltaX = Math.abs(px - targetPx);
				float deltaY = Math.abs(py - targetPy);
				float delta = (float) Math.hypot(deltaX, deltaY) / 4;
				this.vx = (targetPx - px) / delta;
				this.vy = (targetPy - py) / delta;
				orientation = vx > 0;
			}
		}
		s.changeStageTo(STAGE_END);
	}

	private void handleStageEnd(StageModule s, Ticker ticker) {
		setVelocity(vx * ticker.duration, vy * ticker.duration);
		terrainHandler.submitFloatBoxMotion(this);
		attackModule.setDamage(3, DAMAGE_LEVEL_BODY);
	}

	/* **********
	 *   盒子   *
	 ********** */

	/**
	 * 速度, 单位: p/s
	 */
	float vx, vy;

	/**
	 * 由蛋调用, 设置位置.
	 * @param vx
	 *   小鸟的初始横坐标速度, 单位: p / 步
	 * @param vy
	 *   小鸟的初始纵坐标速度, 单位: p / 步
	 */
	void setInitVelocity(float vx, float vy) {
		this.vx = vx;
		this.vy = vy;
		flush();
	}
}
